package com.lindenvalley.ch.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.lindenvalley.ch.R;
import com.lindenvalley.ch.content.Task;


public class TasksArrayAdapter extends ArrayAdapter<Task> implements View.OnTouchListener{
	
	private Context mContext;
	private LayoutInflater inflater;
	private final String TAG = this.getClass().getSimpleName();
	private List<Task> tasks;	
	private final static String taskTimeWhiteSpace = "  ";
	private int resource;
	public TasksArrayAdapter(Context context, int resource, ArrayList<Task> tasks){
		super(context, resource);
		this.mContext = context;
		this.resource = resource;
		this.tasks = tasks;
		Typeface.createFromAsset(mContext.getAssets(), "HelveticaNeueBold.ttf");
		Typeface.createFromAsset(mContext.getAssets(), "HelveticaNeueMedium.ttf");
		mContext.getResources().getString(R.string.buttonTaskTime).concat(taskTimeWhiteSpace);
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
//		Log.i(TAG, ">>>>>>>>> getCount()");		
		return tasks != null ? tasks.size() : 0;
	}
		
	@Override
	public Task getItem (int position){
//		Log.i(TAG, ">>>>>>>>> getItem()");
		try{
			return tasks != null ? tasks.get(Integer.valueOf(position)) : null;
		} catch (IndexOutOfBoundsException ex){
			return null;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		Log.i(TAG, ">>>>>>>>> getView(" + position + ", " + convertView + ", " + parent + ")");
						
		if (tasks == null)
			return null;		
		
		if (convertView == null){
			convertView = inflater.inflate(resource, parent, false);
		}
		Task task = tasks.get(position);
		convertView.setTag(position);
		TextView placeView = (TextView) convertView.findViewById(R.id.dayPlaceView);
		TextView timeView = (TextView) convertView.findViewById(R.id.dayTimeView);
		setBodyText(convertView, task);
		initIconTab(convertView, task);		
		
		String replacedText = null;
		if (task.getPlace() != null && task.getPlace().length() > 0){
			replacedText = String.format(mContext.getResources().getString(R.string.placeText), task.getPlace());
		} else {
			replacedText = String.format(mContext.getResources().getString(R.string.placeText),"");
		}
		placeView.setText(Html.fromHtml(replacedText));
		
		if (task.getTaskTime() != null && task.getTaskTime().length() > 0)
			replacedText = String.format(mContext.getResources().getString(R.string.timeText), task.getTaskTime());
		else
			replacedText = String.format(mContext.getResources().getString(R.string.timeText),"00:00");
		timeView.setText(Html.fromHtml(replacedText));
		
		return convertView;
	}
	
	public void setTasks(List<Task> tasks){
		this.tasks = tasks;
		notifyDataSetChanged();
		notifyDataSetInvalidated();
	}
	
	private void initIconTab(View parent, Task task){
		String value = null;
		TextSwitcher ts = null;
		
		ts = (TextSwitcher) parent.findViewById(R.id.tatigkeitView);		
		ts.setOnTouchListener(this);
		ts.setDisplayedChild(0);
		value = task != null ? task.getTaskText() : null;		
		checkIconTab(ts, value);
		
		ts = (TextSwitcher) parent.findViewById(R.id.berufsschuleView);
		ts.setOnTouchListener(this);
		ts.setDisplayedChild(0);
		value = task != null ? task.getBerufssText() : null;
		checkIconTab(ts, value);
		
		ts = (TextSwitcher) parent.findViewById(R.id.themenView);
		ts.setOnTouchListener(this);
		ts.setDisplayedChild(0);
		value = task != null ? task.getThemenText() : null;
		checkIconTab(ts, value);		
		
		ts = (TextSwitcher) parent.findViewById(task.getActiveTab());
		ts.showNext();
		
	}
	
	/**
	 * @param tv
	 */
	private void checkIconTab(TextSwitcher ts, String value) {
		
		for (int i = 0; i< ts.getChildCount(); i++){
			TextView tv = (TextView) ts.getChildAt(i);
		if (value != null && value.length() > 0) {
			tv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
					.getDrawable(R.drawable.icon_tab_check_active), null, null,null);
			} else {
				tv.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
						.getDrawable(R.drawable.icon_tab_check_inactive), null, null,null);
			}
		}
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		int id = v.getId();
		View parent = (View) v.getParent().getParent();
		Integer tagId = (Integer) parent.getTag();
		Task task = tasks.get(tagId);
		if (event.getAction() == MotionEvent.ACTION_DOWN){
			if (task.getActiveTab() != id){
				TextSwitcher ts;
				ts = ((TextSwitcher)parent.findViewById(task.getActiveTab())); 
				ts.showNext();
				checkIconTab(ts, task.getBodyTextByActiveTab(task.getActiveTab()));
				((TextSwitcher)v).showNext();
				task.setActiveTab(id);
				checkIconTab(((TextSwitcher)v), task.getBodyTextByActiveTab(task.getActiveTab()));
				setBodyText(parent, task);
			}
		}
		return true;
	}

	/**
	 * @param parent
	 * @param task
	 */
	private void setBodyText(View parent, Task task) {
		TextView bodyView = (TextView) parent.findViewById(R.id.dayBodyView);
		String text = task.getBodyTextByActiveTab(task.getActiveTab());
//		Spanned spannedText = null;
//		if (text != null)
//			spannedText = Html.fromHtml(text);
//		bodyView.setText(spannedText);
		bodyView.setText(text);
	}
		

}
